package com.chenfan.mcn.mq.producer;

import com.chenfan.mcn.clients.privilege.PrivilegeClient;
import com.chenfan.mcn.clients.privilege.ScompanyRes;
import com.chenfan.mcn.constant.NumberCst;
import com.chenfan.mcn.dao.StarBasicDataMapper;
import com.chenfan.mcn.dao.StarRebateContractMapper;
import com.chenfan.mcn.enums.FinanceArapTypeEnum;
import com.chenfan.mcn.enums.FinanceChargeTypeEnum;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.model.StarBasicData;
import com.chenfan.mcn.model.StarRebateContract;
import com.chenfan.mcn.mq.AbstractMessageProducer;
import com.chenfan.mcn.mq.MessageProducer;
import com.chenfan.mcn.mq.message.CfChargeCommonMessage;
import com.chenfan.mcn.utils.BusinessAssert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 红人返点合同-消息生产者
 *
 * @author wulg
 * @date 2021-11-19
 **/
@Slf4j
@Component
public class StarRebateContractMessageProducer extends AbstractMessageProducer implements MessageProducer<Long> {

    @Autowired
    private StarRebateContractMapper starRebateContractMapper;

    @Autowired
    private PrivilegeClient privilegeClient;

    @Autowired
    private StarBasicDataMapper starBasicDataMapper;

    @Async
    @Override
    public void asyncSendRecordToMq(Long contractId) {
        log.info("【红人返点合同】开始生成费用MQ消息，contractId={}",contractId);
        StarRebateContract starRebateContract = starRebateContractMapper.selectById(contractId);
        BusinessAssert.notNull(starRebateContract, McnErrorCode.DATA_NOT_FOUND);
        List<ScompanyRes> companyResList = privilegeClient.getParList().getObj();
        Map<Long, String> companyMap = companyResList.stream().collect(Collectors.toMap(k -> k.getCompanyId(), v -> v.getParentCompanyName()));
        CfChargeCommonMessage chargeMessage = new CfChargeCommonMessage();
        chargeMessage.setChargeType(FinanceChargeTypeEnum.OTHER_INCOME.getType());
        chargeMessage.setArapType(FinanceArapTypeEnum.RECEIVE.getType());
        chargeMessage.setChargeSourceCode(starRebateContract.getContractCode());
        chargeMessage.setChargeSourceDetail(starRebateContract.getContractCode());
        chargeMessage.setAmountPp(starRebateContract.getRebateAmount());
        chargeMessage.setPricePp(chargeMessage.getAmountPp());
        if(Objects.nonNull(starRebateContract.getStarId())) {
            StarBasicData starBasicData = starBasicDataMapper.selectById(starRebateContract.getStarId());
            BusinessAssert.notNull(starBasicData, McnErrorCode.STAR_BASIC_DATA_IS_NULL);
            chargeMessage.setBalance(starBasicData.getStarNickname());
        }
        chargeMessage.setFinanceEntity(companyMap.get(starRebateContract.getContractingId()));
        chargeMessage.setCreateBy(starRebateContract.getCreateBy());
        chargeMessage.setCreateName(starRebateContract.getCreateName());
        chargeMessage.setCreateDate(new Date());
        chargeMessage.setUpdateBy(starRebateContract.getCreateBy());
        chargeMessage.setUpdateDate(chargeMessage.getCreateDate());
        chargeMessage.setUpdateName(starRebateContract.getCreateName());
        chargeMessage.setTenantId(starRebateContract.getTenantId());
        chargeMessage.setCompanyId(starRebateContract.getCompanyId());
        chargeMessage.setDepartmentId(starRebateContract.getDepartmentId());
        chargeMessage.setSettTemplate(null);
        chargeMessage.setTaxRate(BigDecimal.ZERO);
        chargeMessage.setAccountId(starRebateContract.getStarBillingId());
        chargeMessage.setBalanceType(NumberCst.ONE);
        boolean result = this.doSendRecordToMq(starRebateContract.getContractCode(), chargeMessage, this.financeTopic);
        if(result) {
            //检查费用生成情况
            this.checkCharge(starRebateContract.getContractCode());
        }
    }
}
